home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk.zip / INIT.C < prev    next >
C/C++ Source or Header  |  1991-04-09  |  4KB  |  203 lines

  1.  
  2. /********************************************
  3. init.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the Awk programming language as defined in
  8. Aho, Kernighan and Weinberger, The AWK Programming Language,
  9. Addison-Wesley, 1988.
  10.  
  11. See the accompaning file, LIMITATIONS, for restrictions
  12. regarding modification and redistribution of this
  13. program in source or binary form.
  14. ********************************************/
  15.  
  16.  
  17. /* $Log:    init.c,v $
  18.  * Revision 2.2  91/04/09  12:39:08  brennan
  19.  * added static to funct decls to satisfy STARDENT compiler
  20.  * 
  21.  * Revision 2.1  91/04/08  08:23:15  brennan
  22.  * VERSION 0.97
  23.  * 
  24. */
  25.  
  26.  
  27. /* init.c */
  28. #include "mawk.h"
  29. #include "code.h"
  30. #include "init.h"
  31. #include "memory.h"
  32. #include "symtype.h"
  33. #include "bi_vars.h"
  34. #include "field.h"
  35.  
  36. #define PROGRAM_FROM_CMDLINE   1
  37.  
  38. /* static protos */
  39. static void PROTO( no_program, (void) ) ;
  40. static void PROTO( process_cmdline , (int, char **) ) ;
  41. static void PROTO( set_FS, (char *) ) ;
  42. static void PROTO( set_dump, (char *) ) ;
  43.  
  44. #if  DOS  &&  ! HAVE_REARGV
  45. #include <fcntl.h>
  46. static  void  PROTO(emit_prompt, (void) ) ;
  47. #endif
  48.  
  49. union tbuff  temp_buff ;
  50. char *main_buff = temp_buff.string_buff + TEMP_BUFF_SZ ;
  51.  
  52. void initialize(argc, argv)
  53.   int argc ; char **argv ;
  54. {
  55.   bi_vars_init() ; /* load the builtin variables */
  56.   bi_funct_init() ; /* load the builtin functions */
  57.   kw_init() ; /* load the keywords */
  58.   field_init() ; 
  59.   process_cmdline(argc, argv)  ;   
  60.  
  61.   jmp_stacks_init() ;
  62.   code_init() ;
  63.   fpe_init() ;
  64.  
  65. #if  NO_STRTOD
  66.   strtod_init() ;
  67. #endif
  68.  
  69. }
  70.  
  71. void  compile_cleanup()
  72. /* program has parsed OK, free some memory
  73.    we don't need anymore */
  74. {
  75.    scan_cleanup() ;
  76.    jmp_stacks_cleanup() ;
  77.    code_cleanup() ;
  78. }
  79.  
  80.  
  81. static void no_program()
  82. { errmsg( 0, "no program") ; mawk_exit(1) ; }
  83.  
  84. int  dump_code ;  /* if on dump internal code */
  85. #ifdef   DEBUG
  86. int  dump_RE   ;  /* if on dump compiled REs  */
  87. #endif
  88.  
  89. static void set_FS(s)
  90.   char *s ;
  91. {
  92.   cell_destroy(field+FS) ;
  93.   field[FS].type = C_STRING ;
  94.   field[FS].ptr = (PTR) new_STRING(s) ;
  95.   cast_for_split( cellcpy(&fs_shadow, field+FS) ) ;
  96. }
  97.  
  98.  
  99. #ifdef  DEBUG
  100. static void set_dump(s)
  101.   char *s ;
  102. {
  103.   while ( 1 )
  104.   { switch ( *s )
  105.     { case 'p' :
  106.       case 'P' :  yydebug = 1 ; break ;
  107.  
  108.       case  'c' :
  109.       case  'C' :  dump_code = 1 ; break ;
  110.  
  111.       case  'r' :
  112.       case  'R' :  dump_RE = 1 ; break ;
  113.  
  114.       case   0  :  
  115.              if ( s[-1] == 'D' ) dump_code = 1 ;
  116.              return ;
  117.  
  118.       default :  break ;
  119.     }
  120.     s++ ;
  121.   }
  122. }
  123. #else
  124. static void  set_dump(s)
  125.   char *s ;
  126. { dump_code = 1 ; }
  127. #endif
  128.  
  129. static void process_cmdline(argc, argv)
  130.   int argc ; char **argv ;
  131. { extern int program_fd ; 
  132.   int i ;  /* index to walk command line */
  133.   char *p ;
  134.   CELL *cp ;
  135.   SYMTAB *st_p ;
  136.   char xbuff[20] ;
  137.  
  138.  
  139.   for( i = 1 ; i < argc && argv[i][0] == '-' ; i++ )
  140.   { p = & argv[i][1] ;
  141.     if ( *p == 'F' )  set_FS(p+1) ;
  142.  
  143.     else if ( *p == 'D' )  set_dump(p+1) ;
  144.     else if ( *p == 'f' )
  145.     { if ( i == argc - 1 )  no_program() ;
  146.       scan_init(! PROGRAM_FROM_CMDLINE, argv[i+1] ) ;
  147.       i += 2 ;
  148.       goto  set_ARGV ;
  149.     }
  150.   }
  151.  
  152. #if  DOS   &&  ! HAVE_REARGV
  153. /* allows short programs to be typed in without mucking stdin */
  154.   emit_prompt() ;
  155.   scan_init(! PROGRAM_FROM_CMDLINE, "CON") ;
  156. #else   /* the real world */
  157.  
  158.   if ( i == argc )  no_program() ;
  159.   scan_init(PROGRAM_FROM_CMDLINE, argv[i]) ;
  160.   i++ ;
  161. #endif
  162.  
  163. set_ARGV:
  164.  
  165.   /* now set up ARGC and ARGV  */
  166.   st_p = insert( "ARGV" ) ;
  167.   st_p->type = ST_ARRAY ;
  168.   Argv = st_p->stval.array = new_ARRAY() ;
  169.   xbuff[0] = '0' ; xbuff[1] = 0 ;
  170.   cp = array_find( st_p->stval.array, xbuff, 1) ;
  171.   cp->type = C_STRING ;
  172.   cp->ptr = (PTR) new_STRING( progname ) ;
  173.  
  174.   /* ARGV[0] is set, do the rest 
  175.      The type of ARGV[1] ... should be C_MBSTRN
  176.      because the user might enter numbers from the command line */
  177.   { int arg_count = 1 ;
  178.  
  179.     for( ; i < argc ; i++, arg_count++ )
  180.     { 
  181.       if ( arg_count < 10 )  xbuff[0] = arg_count + '0' ;
  182.       else (void) sprintf(xbuff, "%u" , arg_count ) ;
  183.       cp = array_find( st_p->stval.array, xbuff, 1) ;
  184.       cp->type = C_MBSTRN ;
  185.       cp->ptr = (PTR) new_STRING( argv[i] ) ;
  186.     }
  187.     bi_vars[ARGC].type = C_DOUBLE ;
  188.     bi_vars[ARGC].dval = (double) arg_count ;
  189.   }
  190. }
  191.  
  192. #if  DOS  &&  ! HAVE_REARGV
  193.  
  194. static void  emit_prompt()
  195. {  static char prompt[] = DOS_PROMPT ;
  196.    int fd = open("CON", O_WRONLY, 0) ;
  197.  
  198.    (void) write(fd, prompt, strlen(prompt)) ;
  199.    (void) close(fd) ;
  200. }
  201. #endif
  202.  
  203.